﻿namespace Dissemination.MVC.Controllers
{
    using System.Web.Mvc;
    using Dissemination.MVC.Repositories;
    using Dissemination.MVC.Models;
    using Dissemination.MVC.ViewModels;
    using System.Web.Security;
    using System;

    public class ReporterManagerController : Controller
    {
        protected TargetAreaRepository repository = new TargetAreaRepository();

        #region List

        //
        // GET: /Program/
        [Authorize(Roles = "Demostrator,Administrator,Reporter")]
        public ActionResult IndexOrDetails(int targetAreaID)
        {
            AccountMembershipService MembershipService = new AccountMembershipService();
            MembershipUser user = MembershipService.GetUserByUserName(User.Identity.Name);
            if (Roles.IsUserInRole("Administrator") || Roles.IsUserInRole("Demostrator"))
            {
                return RedirectToAction("TargetArea", new { id = targetAreaID });
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [Authorize(Roles = "Reporter")]
        public ActionResult Index()
        {
            AccountMembershipService MembershipService = new AccountMembershipService();
            MembershipUser user = MembershipService.GetUserByUserName(User.Identity.Name);
            {
                Guid userID = (Guid)user.ProviderUserKey;

                SurveyResultAndSeminarViewModel viewModel = new SurveyResultAndSeminarViewModel
                {
                    TargetArea = repository.GetByUserID(userID),
                    SurveyResults = repository.GetSurveyResultsByUserID(userID),
                    Seminars = repository.GetSeminarsByUserID(userID)
                };
                return View(viewModel);
            }
        }

        [Authorize(Roles = "Demostrator,Administrator")]
        public ActionResult TargetArea(int id)
        {
            SurveyResultAndSeminarViewModel viewModel = new SurveyResultAndSeminarViewModel
            {
                TargetArea = repository.Get(id),
                SurveyResults = repository.GetSurveyResultsByTargetAreaID(id),
                Seminars = repository.GetSeminarsByTargetAreaID(id)
            };

            return View(viewModel);
        }

        #endregion



    }
}
